home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / art&graf.ix / art-4542 / rshade30 / viewmtv.c < prev    next >
C/C++ Source or Header  |  1993-09-06  |  2KB  |  92 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef enum { FALSE, TRUE } boolean;
  5.  
  6. typedef unsigned char BYTE;
  7.  
  8.  
  9.    /* Interface to John Logajan's gray-level display routines. */
  10.  
  11. extern void grayinit(void), grayexit(void), grayshow(void);
  12. extern void gray22(BYTE *address, long span);
  13.  
  14.  
  15. boolean read_file(FILE *fp, BYTE *mem, int w, int h)
  16. {
  17.    BYTE *rbuf, *rover;
  18.    int x, y;
  19.    long conv;
  20.  
  21.    if ((rbuf = calloc((size_t)w, (size_t)3)) == NULL)
  22.       return FALSE;
  23.  
  24.    for (y = 0; y < h; ++y) {
  25.       if (fread(rbuf, (size_t)3, (size_t)w, fp) < (size_t)w) {
  26.          free(rbuf);
  27.          return FALSE;
  28.       }
  29.  
  30.       for (x = 0, rover = rbuf; x < w; ++x) {
  31.          conv   = *rover++ * 299l       /* calc luminance (NTSC Y formula) */
  32.                 + *rover++ * 587l
  33.                 + *rover++ * 114l;
  34.          *mem++ = (BYTE)(conv / 11591); /* scale to range 0..21 */
  35.       }
  36.    }
  37.  
  38.    free(rbuf);
  39.  
  40.    return TRUE;
  41. }
  42.  
  43. void main(int argc, char **argv)
  44. {
  45.    FILE *file;
  46.    int width, height;
  47.    BYTE *buffer;
  48.  
  49.    if (argc != 2) {
  50.       printf("Usage: viewmtv filename\n");
  51.       exit(1);
  52.    }
  53.  
  54.    if ((file = fopen(argv[1], "rb")) == NULL) {
  55.       printf("Can't open '%s'\n", argv[1]);
  56.       exit(1);
  57.    }
  58.  
  59.    if (fscanf(file, "%d %d", &width, &height) != 2) {
  60.       fclose(file);
  61.       printf("'%s' is not in MTV format\n");
  62.       exit(1);
  63.    }
  64.  
  65.    while (getc(file) != '\n') /* skip MTV `header' line */
  66.       ;
  67.  
  68.    if ((buffer = malloc((long)width * (long)height)) == NULL) {
  69.       fclose(file);
  70.       printf("Not enough memory for %dx%d pixels\n", width, height);
  71.       exit(1);
  72.    }
  73.  
  74.    if (! read_file(file, buffer, width, height)) {
  75.       fclose(file);
  76.       printf("Error while reading '%s'\n", argv[1]);
  77.       exit(1);
  78.    }
  79.  
  80.    fclose(file);
  81.  
  82.    /* ### */
  83.    grayinit();
  84.    gray22(buffer, width);
  85.    grayexit();
  86.    /* ### */
  87.  
  88.    free(buffer);
  89.    exit(0);
  90. }
  91.  
  92.